<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Roles extends Admin_Controller
{
	private $rules = array(
		'id'					=>'trim'
		, 'name' 				=> 'trim|required|ma_length[100]'
		, 'description' 		=> 'trim|ma_length[500]'
		, 'parent_id'			=> 'trim'
	);

	function __construct()
	{
		parent::Admin_Controller();
    	//Load resources
		$this->load->library('session');
		$this->load->library('validation');
		$this->load->model('roles_m');
		$this->lang->load('authorizations');
	}

	// list danh sach nhom tai khoan
  	function index()
	{
		$roles = $this->roles_m->getRoles(array());
		$this->data->roles = $roles;
		if ($this->input->post('Add')){
			redirect('admin/authorizations/admin_roles/create');
		} else if ($this->input->post('Delete')) {
			$id_array = ($id > 0) ? array($id) : $this->input->post('action_to');
		    if (empty($id_array)){
				$this->session->set_flashdata('error', $this->lang->line('role_delete_error'));
				redirect('admin/authorizations/admin_roles/index');
			}

			foreach($id_array as $id){
				$this->roles_m->deleteRole($id);
			}

			$this->session->set_flashdata('success', $this->lang->line('role_delete_success'));
			redirect('admin/authorizations/admin_roles/index');
		} else if ($this->input->post('Edit')) {
			$ids = $this->input->post('action_to');
			if (isset($ids) && $ids && count($ids) == 1) {
				redirect('admin/authorizations/admin_roles/edit/'.$ids[0]);
			} else {
				$this->session->set_flashmessage( 'error', $this->lang->line ( 'role_mass_edit_error' ) );
			}
		}
		$this->data->toolbar = createToolbar ( array (array ('command' => 'Add' ), array ('command' => 'Edit'), array ('command' => 'Delete')), array ('label' => $this->lang->line ( 'role_manage_title' ), 'attribute' => 'class = "acc_permission toolbar_title"' ) );
		$this->layout->create('admin/roles/roles', $this->data);
	}

	function edit($id = 0)
	{
		if (!$id) redirect('admin/authorizations/admin_roles/index');
		$role = $this->roles_m->getRole(array('id'=>$id));
		$this->data->role = $role;
        $this->load->library('validation');
		$rules = array(
			'id'					=>'trim'
			, 'description' 		=> 'trim|max_length[500]'
			, 'parent_id'			=> 'trim'
		);
		$this->validation->set_rules($rules);

		$fields['name'] = $this->lang->line('role_name_label');
		$fields['description'] = $this->lang->line('role_description_label');
		$fields['parent_id'] = $this->lang->line('role_parent_id_label');
		$this->validation->set_fields($fields);

		//foreach(array_keys($this->rules) as $field) {
//			if(isset($_POST[$field])) $this->data->role->$field = $this->validation->$field;
//		}

		if ($this->input->post('save')){
			if ($this->validation->run())
			{
				// Check role name conflict
				$nameConflict = false;
//				foreach($this->data->otherRoles as $otherRoleName){
//					if ($this->data->role->name === $otherRoleName){
//						$nameConflict = true;
//						break;
//					}
//				}

				if ($nameConflict){
					$this->session->set_flashdata('error', $this->lang->line('role_name_conflict'));
				} else {
					$input = array();
					$input['id'] = $_POST['id'];
					$input['description'] = $_POST['description'];
					$input['parent_id'] = $_POST['parent_id'];
					$this->roles_m->updateRole($id, $input);
					$this->session->set_flashdata('success', $this->lang->line('role_update_success'));
					redirect('admin/authorizations/admin_roles/index');
				}
			}
		} else if ($this->input->post('cancel')) {
			redirect('admin/authorizations/admin_roles/index');
		}

		// Get other roles
		$this->data->otherRoles[''] = '';
		foreach( $this->roles_m->getOtherRoles($role->name) as $row){
			$this->data->otherRoles[$row->name] = $row->name;
		}

		$this->data->toolbar = createToolbar ( array (array ('command' => 'save' ), array ('command' => 'cancel', 'attribute'=>'class="btn_admin"')), array ('label' => sprintf($this->lang->line ( 'role_edit_title' ), $this->data->role->name), 'attribute' => 'class = "acc_permission toolbar_title"' ) );
		$this->layout->create('admin/roles/form', $this->data);
	}

	function delete($id = 0){
		// An ID was passed in the URL, lets delete that
		$id_array = ($id > 0) ? array($id) : $this->input->post('action_to');

		if (empty($id_array)){
			$this->session->set_flashdata('error', $this->lang->line('role_delete_error'));
			redirect('admin/authorizations/admin_roles/index');
		}

		foreach($id_array as $id){
			$this->roles_m->deleteRole($id);
		}

		$this->logger->info('Xoa role, ids = '.implode(',', $id_array), USER_ACTION_CONF);
		$this->session->set_flashdata('success', $this->lang->line('role_delete_success'));
		redirect('admin/authorizations/admin_roles/index');
	}

	function create(){
		$this->validation->set_rules($this->rules);

		if($this->input->post('save')){
			if ($this->validation->run()){
				//Check if role existed
				if (count($this->roles_m->getRole(array('name'=>$_POST['name']))) == 0){
					$id = $this->roles_m->newRole($_POST);
					$this->setAccountLogger('Them moi role '.$_POST['name']);
					$this->session->set_flashdata('success', $this->lang->line('role_create_success'));
					redirect('admin/authorizations/admin_roles/index');
				} else {
					//$this->setAccountLogger('Them moi role '.$_POST['name'].'đã tồn tại');
					$this->session->set_flashmessage('error', $this->lang->line('role_existed_error_message'));
				}
			}
		} else if ($this->input->post('cancel')){
			redirect('admin/authorizations/admin_roles/index');
		}

		foreach(array_keys($this->rules) as $field)
		{
			$this->data->role->$field = isset($_POST[$field]) ? $_POST[$field] : '';
		}

		$this->data->otherRoles[''] = '';
		foreach( $this->roles_m->getOtherRoles(0) as $row){
			$this->data->otherRoles[$row->name] = $row->name;
		}

		$this->data->toolbar = createToolbar ( array (array ('command' => 'save' ), array ('command' => 'cancel', 'attribute'=>'class="btn_admin"')), array ('label' => $this->lang->line ( 'role_create_title' ), 'attribute' => 'class = "acc_permission toolbar_title"' ) );
		$this->layout->create('admin/roles/form', $this->data);
	}

	/*
	 * Edit role's user list
	 */
	function editUsers($id = 0){
		if (!$id){
			$id = $_POST['role_id'];

		if (!$id)
			redirect('admin/authorizations/admin_roles/index');
		}

		$role = $this->roles_m->getRole(array('id'=>$id));
		if($this->input->post('save')){
			$id_array = $this->input->post('action_to');
			$this->roles_m->updateUserRole($id_array, $role->name);
			//$this->logger->info('Cap nhat danh sach user đoi voi role, id = '.$id, USER_ACTION_CONFIG_SYTEM);
			$this->session->set_flashdata('success', $this->lang->line('role_useredit_update_success_message'));
			redirect('admin/authorizations/admin_roles/index');
		} else if ($this->input->post('cancel')) {
			redirect('admin/authorizations/admin_roles/index');
		}


		$this->data->role = $role;

		$filter_status = 0;
		if ($_POST)
			$filter_status = $_POST['filter_status'];
		$this->data->filter_status = $filter_status;

		$this->data->users = $this->roles_m->getUsers($role->name, $filter_status);
		$filter_statuses = array('0'=>$this->lang->line('all_user')
						, '1'=>$this->lang->line('user_inside_role')
						, '2'=>$this->lang->line('user_outside_role'));
		$this->data->filter_statuses = $filter_statuses;

		$this->data->toolbar = createToolbar(array(array('command' => 'save' ), array ('command' => 'cancel', 'attribute'=>'class="btn_admin"')), array ('label' => sprintf($this->lang->line('role_user_manage_title'), $role->name), 'attribute' => 'class = "acc_permission toolbar_title"' ));
		$this->layout->create('admin/roles/users', $this->data);
	}
}
?>